<?php
namespace app\python\controller;  // 必须继承于app 第二个是具体的应用名  例 t5/note/tp
use think\Controller;
//use think\Request;


use think\config;


// use think\Db;
class Index extends Controller
{
/*
*https://www.hyhthjk.com.cn/note.php/python/Index/index
django 的安装
*/

public function index(){
echo'
<pre>
https://www.hyhthjk.com.cn/note.php/python/Index/index
  添加环境变量
  在https://www.djangoproject.com/download/ 的第二大项 找 pip install Django==2.1.4 （版本号可能已经改变）  复制这句
  黏贴在cmd 里 python下 回车 就行 

</pre>
';
}

/**
 * 第一张页面
 */
public function index2(){
echo'
<pre>
见 http://127.0.0.1:8000/index/
在 E:\python3\myblog\myblog1\blog\views.py中
from django.shortcuts import render
from django.http import HttpResponse 

def index(request):
    return HttpResponse(\'HELLO, world\')

<hr/>
在 E:\python3\myblog\myblog1\myblog1\urls.py
from django.contrib import admin
from django.urls import path
import blog.views as bv

urlpatterns = [
    path(\'admin/\', admin.site.urls),
    path(\'index/\',bv.index),
]
</pre>
';
}

/**
 * 配置多层urls
 */
public function index3(){
echo'
地址栏：http://127.0.0.1:8000/blog/index/
<pre>
在 E:\python3\myblog\myblog1\myblog1\urls.py中
from django.contrib import admin
#from django.urls import path
from django.urls import include, path
import blog.views as bv

urlpatterns = [
    path(\'admin/\', admin.site.urls),
    path(\'blog/\', include(\'blog.urls\')),
    path(\'blog2/\', include(\'blog2.urls\')),
]

<hr/>
在 E:\python3\myblog\myblog1\blog2\urls.py
from django.urls import path

from . import views

urlpatterns = [
    path(\'index/\', views.index),
]
<hr/>
在 E:\python3\myblog\myblog1\blog2\views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    #不使用html
    # return HttpResponse(\'HELLO, world E:\python3\myblog\myblog1\ blog\ views.py\')
    #使用html templates/blog/index
    return render(request, \'blog2/index.html\'{\'hello\':\'向html传参数\'})

<hr/>
在E:\python3\myblog\myblog1\blog2\templates\blog2\index.html
<h2>{{hello}}</h2>


</pre>
';

}


/**
 * MODEL层
 */
public function index4(){
  echo'
  <pre>
  <h3>model层 E:\python3\myblog\myblog1\blog\models.py</h3>
  from django.db import models

  class Article(models.Model):
      title = models.CharField(max_length=32,default=\'Title\')
      content = models.TextField(null=True)

      #在admin表的标题里返回标题的值
      def __str__(self):
          return self.title
<hr/>
<h3>生成数据表</h3>
在cmd中 
第一步 E:\python3\myblog\myblog1>python manage.py makemigrations  // 创建
第二步 E:\python3\myblog\myblog1>python manage.py migrate         // 数据迁移 生成 E:\python3\myblog\myblog1\blog\migrations\0001_initial.py
第三步 E:\python3\myblog\myblog1>python manage.py sqlmigrate blog 0001 //生成数据库


  </pre>
  ';
}

/**
 * 从数据库获得多个数据 向前台渲染 
 */
public function index5(){
  echo'
  <pre>
  <h3>views  E:\python3\myblog\myblog1\blog\views.py</h3>
  from django.shortcuts import render
from django.http import HttpResponse

<red>from . import models</red>

def index(request):
    # return HttpResponse(\'HELLO, world<br/> E:\python3\myblog\myblog1\ blog\ views.py\')
    #Article 数据表名
    article = models.Article.objects.get(pk=1)
    return render(request, \'blog/index.html\',{\'hello\':article,\'answer\':\'从后台传来\'})
    <hr/>
    <h3>html E:\python3\myblog\myblog1\blog\templates\blog\index.html</h3>
    <h2>{{answer}}......{{hello.title}}.......{{hello.content}}</h2>
  </pre>
  ';
}


/**
 * 向admin添加项目管理 和数据表 管理
 */
public function index6(){
echo'
<pre>
<h3>在 E:\python3\myblog\myblog1\blog\admin.py</h3>
from django.contrib import admin

from .models import Article

admin.site.register(Article)

</pre>
';

} 


/**
 * 获得数据表中的全部数据 向前台渲染
 */
public function index7(){
  echo'
<pre>
  <h3>E:\python3\myblog\myblog1\blog2\views.py </h3>

  def index2(request):
  #获取Article表的全部数据 向前台渲染  all 是全部取出
  articles = models.Article.objects.all()
  return render(request, \'blog/index2.html\',{\'articles\':articles})
 
  <hr/>
  <h3>E:\python3\myblog\myblog1\blog\templates\blog\index2.html </h3>
  {% for article in articles %}
	<a href="">{{article.title}}....{{article.content}}</a><br/>
    {% endfor %}

  <hr/>
   <h3> E:\python3\myblog\myblog1\blog\urls.py</h3>
   from django.urls import path

   from . import views
   
   urlpatterns = [
       path(\'index/\', views.index),
       path(\'index2/\', views.index2),
   ]

</pre>
';
}

/**
 *无参数 无html
 */
public function index8(){
echo'
<pre>
<h3>演示 http://127.0.0.1:8000/blog3/index/</h3>
<h3>E:\python3\myblog\myblog1\blog3\urls.py </h3>
from django.urls import path

from . import views

urlpatterns = [
    path(\'index/\', views.index),
]
<hr/>
<h3> E:\python3\myblog\myblog1\blog3\views.py </h3>
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse(\'这是后台 E:\python3\myblog\myblog1\ blog3\ views.py\')
</pre>
';
}

/**
 * get传值  两个参数
 */
public function index9(){
  echo'
  <pre>
  <h3> 地址栏 http://127.0.0.1:8000/blog/args/?user=张飒&pwd=123</h3>
  输出：账号:张飒,密码:123
  <hr/>
  <h3>E:\python3\myblog\myblog1\blog\urls.py </h3>
  from django.urls import path

  from . import views
  
  urlpatterns = [
      path(\'args/\',views.params_first),
<hr/>
<h3> E:\python3\myblog\myblog1\blog\views.py </h3>
from django.shortcuts import render
from django.http import HttpResponse

from . import models

def params_first(request):
    #http://127.0.0.1:8000/blog/args/?user=张飒&pwd=123
    user_name=request.GET.get(\'user\',\'\')
    pass_word=request.GET.get(\'pwd\',\'\')
    result=\'账号:{},密码:{}\'.format(user_name,pass_word)
    return HttpResponse(result)
  </pre>
  ';
}

/**
 * get传值 两个参数 无键名 只有键值
 */
public function index10(){
echo'
<pre>
<h3> 地址 http://127.0.0.1:8000/blog/params/%E5%BC%A0%E9%A3%92/123/</h3>
输出：name=张飒,pwd=123
<hr/>
<h3> E:\python3\myblog\myblog1\blog\urls.py</h3>
from django.urls import path,re_path

from . import views

urlpatterns = [
    path(\'args/\',views.params_first),
    re_path(r\'^params/(\w+)/(\d+)/$\',views.params),
]
<hr>
<h3> E:\python3\myblog\myblog1\blog\views.py </h3>
def params(request,name,pwd):
    result=\'name={},pwd={}\'.format(name,pwd)
    return HttpResponse(result)

</pre>
';

}

/**
 * get传值 两个参数 无键名 只有键值 方法二 推荐
 */
public function index11(){
echo'
<pre>
<h3>地址http://127.0.0.1:8000/blog/params1/张第三/1423/ </h3>
输出name=张2飒,pwd=1423
<hr/>
<h3> E:\python3\myblog\myblog1\blog\urls.py</h3>
from django.urls import path

from . import views

urlpatterns = [
    path(\'args/\',views.params_first),
    #re_path(r\'^params/(\w+)/(\d+)/$\',views.params),
    #把re_path 转成path 的写法
    path(\'params1/&lt;username&gt;/&lt;password&gt;/\',views.params1),
<hr/>
<h3> E:\python3\myblog\myblog1\blog\views.py </h3>
def params1(request,username,password):
    #http://127.0.0.1:8000/blog/params1/张发给/1423/
    result=\'name={},pwd={}\'.format(username,password)
    return HttpResponse(result)

</pre>
';

} 


/**
 * 超链接 用a标签传参数 给后台
 */

public function index12(){
echo'
<pre>
案例 地址 http://127.0.0.1:8000/blog/index3/
<hr/>
<h3>项目配置 加命名空间 E:\python3\myblog\myblog1\myblog1\urls.py</h3>
from django.contrib import admin
from django.urls import include, path
import blog.views as bv

urlpatterns = [
    path(\'admin/\', admin.site.urls),
    # path(\'index/\',bv.index),
    path(\'blog/\', include(\'blog.urls\',namespace=\'blog\')),
]

<hr/>
<h3>  E:\python3\myblog\myblog1\blog\urls.py</h3>
from django.urls import path,re_path

from . import views

<span style="font-color:red">#配合namespace=blog</span>
app_name=\'blog\'

urlpatterns = [
    path(\'index/\', views.index),
    path(\'index2/\', views.index2),
    #有a标签
    path(\'index3/\', views.index3,name=\'index3\'),
    #通过a标签 被链接到index
    path(\'index/<article_id>/\', views.index4,name=\'index4\'),  
]
<hr/>
<h3> E:\python3\myblog\myblog1\blog\templates\blog\index3.html </h3>
{% for article in articles %}
&lt;a href="{% url \'blog:index4\' article.id %}">{{article.id}}....{{article.title}}....{{article.content}}&gt;&lt;/a&gt;&lt;br/&gt;
{% endfor %}
<hr/>
<h3>后台接到 参数 E:\python3\myblog\myblog1\blog\views.py </h3>
def index4(request,article_id):
    #result=\'id={}\'.format(article_id);
    id = format(article_id)
    article = models.Article.objects.get(pk=id)
    return render(request, \'blog/index.html\', {\'hello\': article, \'answer\': \'从index4的后台传来\'})



</pre>
';
} 

/**
 * 函数 页面 重定义页面
 */
public function index13(){
echo'
<pre>
案例地址 http://127.0.0.1:8000/blog/shiyan/
函数index5 用show.html 页面 但是由于定义了path为 shiyan 所以 地址栏要写http://127.0.0.1:8000/blog/shiyan/
<hr/>
<h3> E:\python3\myblog\myblog1\blog\views.py </h3>
def index5(request):
    data = \'函数index5 用show.html 页面 但是由于定义了path为 shiyan 所以 地址栏要写http://127.0.0.1:8000/blog/shiyan/\'
    return render(request, \'blog/show.html\',{\'data\':data})
<hr/>
<h3> E:\python3\myblog\myblog1\blog\templates\blog\show.html <h3>
{{data}}
<hr/>
<h3> E:\python3\myblog\myblog1\blog\urls.py </h3>
path(\'shiyan/\',views.index5),


</pre>
';
}

/**
 * 表单和提交到数据库
 */
public function index14(){
echo'
<pre>
<h3>实例 地址 http://127.0.0.1:8000/blog/edit/</h3>
<hr/>
<h3>页面  表单提交给 E:\python3\myblog\myblog1\blog\views.py 的 edit_action 函数处理</h3>
&lt;form action="{% url \'blog:edit_action\' %}" method="post"&gt;
<hr/>
<h3>E:\python3\myblog\myblog1\blog\views.py 的 edit_action </h3>

def edit_action(request):
    title = request.POST.get(\'title\',\'默认值：标题\')
    content = request.POST.get(\'content\',\'默认值：内容\')
    models.Article.objects.create(title=title,content=content)
    articles = models.Article.objects.all()
    return render(request,\'blog/index.html\',{\'articles\':articles})

</pre>
';
}

/***
 * 创建mysql 数据表环境
 */
public function index15(){
echo'
<pre>
import mysql.connector

def index3(request):
    mydb = mysql.connector.connect(
        host="localhost",  # 数据库主机地址
        user="root",  # 数据库用户名
        passwd="",  # 数据库密码
        database="test"
    )
    mycursor = mydb.cursor()
    mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
    return HttpResponse(\'ok\')
</pre>
';
}

/**
 * 查询表 遍历到页面
 */

public function index16(){
    echo'
    <pre>
    <h3>E:\python3\myblog\myblog1\blog2\views.py </h3>
    import mysql.connector

    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        passwd="",
        database="note"
    )
    mycursor = mydb.cursor()
    mycursor.execute("SELECT * FROM note_test")
    myresult = mycursor.fetchall()  # fetchall() 获取所有记录
    mycursor.close()
    mydb.close()

    return render(request, \'blog2/index4.html\', {\'articles\': myresult})


    <hr/>
    <h3> E:\python3\myblog\myblog1\blog2\templates\blog2\index4.html </h3>
    {% for article in articles %}
	{{article.0}}....{{article.1}}<br/>

    {% endfor %}
    </pre>
    ';
}

/**
 * 添加css
 */

 public function index17(){
echo'
<pre>
在manager.py同层级下创建static文件夹, 里面放上css , js, image等文件或者文件夹
<hr/>
在settings.py中进行设置就行, 在末尾添加以下代码
STATIC_URL = \'/static/\'
HERE = os.path.dirname(os.path.abspath(__file__))
HERE = os.path.join(HERE, \'../\')
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don\'t forget to use absolute paths, not relative paths.
    os.path.join(HERE, \'static/\'),
)
<hr/>
&lt;link rel="stylesheet" href="../static/style/medicine.css" /&gt;


</pre>
';
 }

/**
 * 第一张无html页面
 */
public function index18(){
echo'
<pre>
  http://127.0.0.1:8000/blog/index/
<h6>在 D:\python3www\myblog\myblog\urls.py</h6> 
from django.contrib import admin
from django.urls import include, path
import blog.views as bv

urlpatterns = [
    path(\'admin/\', admin.site.urls),
    #path(\'index/\',bv.index),
    path(\'blog/\', include(\'blog.urls\', namespace=\'blog\')),
]
<hr/>
<h6>在 D:\python3www\myblog\blog\urls.py </h6>
from django.urls import path,re_path

from . import views

#配合namespace=blog
app_name=\'blog\'

urlpatterns = [
    path(\'index/\', views.index),
]
<hr/>
<h6>在D:\python3www\myblog\blog\views.py </h6>
from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def index(request):
    return HttpResponse(\'hello world\')




</pre>
';
}
/**
 * 第一张有html页面
 */
public function index19(){
    echo'
    <pre>
       http://127.0.0.1:8000/blog/page2/
    <h6>在D:\python3www\myblog\myblog\urls.py </h6>
    from django.contrib import admin
    from django.urls import include, path
    import blog.views as bv
    
    urlpatterns = [
        path(\'admin/\', admin.site.urls),
        path(\'blog/\', include(\'blog.urls\', namespace=\'blog\')),
    ]

  <h6> 在 D:\python3www\myblog\blog\urls.py </h6>
  from django.urls import path,re_path
  from . import views
  
  #配合namespace=blog
  app_name=\'blog\'
  
  urlpatterns = [
      path(\'page2/\', views.index2),
  ]

  <h6>在 D:\python3www\myblog\blog\views.py </h6>
  from django.shortcuts import render
 from django.http import HttpResponse

 def index2(request):
    return render(request, \'blog/index.html\')

    <h6> 在D:\python3www\myblog\blog\templates\blog\index.html </h6>
     写html

    </pre>
    ';
    }
}